# 对比两个模型并画图，放进ppt里
import matplotlib.pyplot as plt
import numpy as np

from model import loadAgent


model_path1 = './model/4tuple-epoch-20000.pkl'
model_path2 = './model/4tuple-epoch-22000.pkl'

if __name__ == '__main__':
    agent1 = loadAgent(model_path1)
    agent2 = loadAgent(model_path2)

    tuple_weight1 = agent1.Tuples[1].V
    tuple_weight2 = agent2.Tuples[1].V

    print(tuple_weight1.size)
    print(tuple_weight2.size)

    res = [list() for _ in range(15)]

    for a1 in range(15):
        for a2 in range(15):
            for a3 in range(15):
                for a4 in range(15):
                    max_num = max(a1, a2, a3, a4)
                    key = (a1, a2, a3, a4)
                    val1 = tuple_weight1[key]
                    val2 = tuple_weight2[key]
                    res[max_num].append(abs(val1-val2))

    xs = list(range(4, 13))

    fig = plt.figure()
    ax1 = fig.add_subplot(111)

    # 绘图state的总数量
    ax1.bar(xs, [len(res[x]) for x in xs], color='orange')
    ax1.set_ylabel('num of states')
    # plt.grid()
    # plt.savefig('./plt/nums.png')
    ax2 = ax1.twinx()

    # 绘图state的更新比例
    # plt.clf()
    ax2.plot(xs, [len([y for y in res[x] if y != 0]) / len(res[x]) for x in xs])
    ax2.scatter(xs, [len([y for y in res[x] if y != 0]) / len(res[x]) for x in xs])
    # plt.xlabel('max number in tuple')
    ax2.set_ylabel('ratio of states seen in 2000 games')
    # plt.xticks(xs, [2**x for x in xs])
    plt.xticks(xs, [2**x for x in xs])
    plt.xlabel('max number in tuple')
    plt.grid()
    plt.savefig('./plt/update_ratios.png')
